package com.lovo.invoicing.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lovo.invoicing.bean.LogBean;
import com.lovo.invoicing.bean.PageBean;
import com.lovo.invoicing.bean.UserBean;
import com.lovo.invoicing.service.ILogService;
import com.lovo.invoicing.service.IUserService;
import com.lovo.invoicing.service.MSGService;
import com.lovo.invoicing.utils.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    IUserService userService;
    @Autowired
    ILogService logService;

    @Autowired
    MSGService msgService;

    @RequestMapping("/userLogin")
    public int[] userLogin(@RequestBody UserBean userBean, HttpServletRequest request){
       UserBean u =  userService.queryUser(userBean);
       if (u!=null){
           //session
           request.getSession().setAttribute("user",u);
           //日志
           logService.addLog(u.getUserName(),"登录系统");
           List<Integer> list =userService.queryLimitsByUserId(u.getUserId());
           //去重
           list = list.stream().distinct().collect(Collectors.toList());
           return   list.stream().mapToInt(Integer::intValue).toArray();
       }
       return null;
    }
    //获取验证码
    @RequestMapping("/getCode")
    public String getCode(String phoneNumber){
        UserBean userBean =  userService.queryUserByPhoneNumber(phoneNumber);
        if (userBean==null){
            return null;
        }
        String code = RandomUtil.getFourBitRandom();
        Map map = new HashMap();
        map.put("code",code);
        boolean b = msgService.send(map,phoneNumber);
        if (b){
            return code;
        }
        return null;
    }

    @RequestMapping("/loginByPhoneNumber")
    public int[] loginByPhoneNumber (String phoneNumber,HttpServletRequest request){

        UserBean u =  userService.queryUserByPhoneNumber(phoneNumber);
        if (u!=null){
            //session
            request.getSession().setAttribute("user",u);
            //日志
            logService.addLog(u.getUserName(),"登录系统");
            List<Integer> list =userService.queryLimitsByUserId(u.getUserId());
            //去重
            list = list.stream().distinct().collect(Collectors.toList());
            return   list.stream().mapToInt(Integer::intValue).toArray();
        }
        return null;
    }


   @RequestMapping("/findRealName")
    public String findRealName(HttpServletRequest request){
        UserBean u =(UserBean) request.getSession().getAttribute("user");
        return u.getRealName();
   }

    @RequestMapping("/addUser")
    public String addUser(String userName,String realName,int[] roleIds){
        if (roleIds.length==0){
            return "error";
        }
        userService.addUser(userName,realName,roleIds);
        return "success";
    }

    @RequestMapping("/removeUser")
    public String removeUser(int userId){
      userService.deleteUser(userId);
      return "success";
    }

    //修改用户权限
    @RequestMapping("/modifyUser")
    public String modifyUser(int userId,int[] roleIds){
        if (roleIds.length==0){
            return "error";
        }
        userService.updateUser(userId,roleIds);
        return "success";
    }

    @PostMapping("/findUsersByPage")
    public IPage<UserBean> findUsersByPage(@RequestBody PageBean<UserBean> pageBean){
        return userService.queryUsersByPage(pageBean);
    }

    @RequestMapping("/comparePassWord")
    public String comparePassWord(String oldPwd,HttpServletRequest request){
      UserBean userBean =(UserBean) request.getSession().getAttribute("user");
      String passWord =  userService.queryPassWordById(userBean.getUserId());
        if (passWord.equals(oldPwd)){
          return "success";
      }
      return "error";
    }

    @RequestMapping("/modifyPassWord")
    public String modifyPassWord(String newPwd,HttpServletRequest request){
        UserBean u =(UserBean) request.getSession().getAttribute("user");
        u.setPassWord(newPwd);
        userService.updatePassWord(u);
        return "success";
    }
}
